package com.gy.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class ThreadPoolConfig {

    @Bean
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        int coreThreads = Runtime.getRuntime().availableProcessors();
        executor.setCorePoolSize(coreThreads);
        executor.setMaxPoolSize(coreThreads * 2);
        executor.setQueueCapacity(0); // 使用 SynchronousQueue，避免排队
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("upload-task-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝策略：调用者运行
        executor.initialize();
        return executor;
    }
}
